package com.chencs.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chencs.app.xcx.WirelessShareTpwdQueryRequest;
import com.chencs.biz.Common;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class TaoKouLingUtils {


    /**
     * 淘宝口令解析
     * @param str
     * @return
     */
    public static  Map<String,String> getTaoKouLing(String str){

        String str2 = new String(TaoKouLingUtils.decode(str));
        TaoKouLing taoKouLing = null;
        Map<String,String> map2 = new HashMap<>();
        try {
            TaobaoClient client = new DefaultTaobaoClient(Common.apiUrl + "taobao.wireless.share.tpwd.query", Common.appKey, Common.appSecre);
            WirelessShareTpwdQueryRequest req = new WirelessShareTpwdQueryRequest();
            req.setPasswordContent(str2);
            String rsp = client.execute(req).getBody();
            System.out.println(rsp);
            JSONObject obj = JSON.parseObject(rsp).getJSONObject("wireless_share_tpwd_query_response");
            taoKouLing = JSONObject.parseObject(obj.toJSONString(),TaoKouLing.class);
            if(!taoKouLing.getSuc()){
                return  null;
            }
            Map<String, String> map =   CRequest.URLRequest(taoKouLing.getUrl());
            map2 = ApplyCommissionUtils.getSiyouquan(map.get("id"));
            map2.put("pid",map.get("id"));
            map2.put("img",taoKouLing.getPic_url());
            map2.put("price",taoKouLing.getPrice());
            map2.put("title",taoKouLing.getContent());
        }catch (Exception e){
            System.out.println(str);
            e.printStackTrace();
            return  null;
        }
        return  map2;
    }

    public static byte[] decode(String str){
        byte[] bt = null;
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            bt = decoder.decodeBuffer( str );
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bt;
    }


   public static void main(String[] args) {

//        Map<String,String>  map2 =  TaoKouLingUtils.getTaoKouLing("77+lVFRLSDB3dVlMSXrvv6U=");
//
//        System.out.println("dddd"+map2.toString());
//



   }





}
